import string
import random

class bnfBlock:
	def __init__(self, lineIn):
		# Pre: 	take in a line from a bnf file
		# Post:	internalize the structure of the line
		hook = "::="
		assgLoc = string.find(lineIn, hook) 
		self.pre = string.strip(lineIn[:assgLoc])
		post = string.strip(lineIn[assgLoc + len(hook):])
		self.parsePosts(post)

	def getName(self):
		return self.pre

	def getTrans(self):
		# Pre:	none
		# Post: 	return a random transition
		return random.choice(self.toTransList)
	
	def parsePosts(self, lineIn):
		# Pre:	take in a line of pipe delimited strings
		# Post:	internalize the strings as items in a list
		self.toTransList = []
		itemList = string.split(lineIn, '|')
		for item in itemList:
			self.toTransList.append(string.strip(item))		

	def __repr__(self):
		return "%s->%s" % (self.getName(), string.join(self.toTransList, '|')) 

if __name__=="__main__":
	bnf = bnfBlock("<S> ::= a<element> | a<B> | b<element>")
	print bnf.getName()
	print bnf.getTrans()
